import request from "@/utils/request";
import { get } from "https";
import { myBrowser } from "@/utils/mix.js";

const env = window.ENV || {
  API_LOG: "",
};

//获取登录日志接口
export function getLoginLog(query) {
  return request({
    url: env.API_LOG + `/v1/logs/logins`,
    method: "get",
    params: query
  });
}


//接口服务日志
export function getInterfaceLog(query) {
  return request({
    url: env.API_LOG + `/v1/logs/apiservices`,
    method: "get",
    params: query
  });
}
//操作日志接口
export function getOperationsLog(query) {
  return request({
    url: env.API_LOG + `/v1/logs/operations`,
    method: "get",
    params: query
  });
}
//日志导出
export function importLog(type, query) {
  let url = "";
  let name = "";
  if (type == "1") { //登录
    url = env.API_LOG + `/v1/logs/logins`;
    name = "登录日志";
  } else if (type == "2") { //操作日志
    url = env.API_LOG + `/v1/logs/operations`;
    name = "操作日志";
  } else if (type == "3") { //接口日志
    url = env.API_LOG + `/v1/logs/apiservices`;
    name = "接口服务日志";
  }
  var browser = myBrowser();
  if (!browser) {
    browser = "IE";
  }
  request({
    url: url,
    method: "GET",
    params: query,
    responseType: "blob"
  }).then(res => {
    var blob = new Blob([res.data], {
      //type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8"
    });
    var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
    var isIE =
      userAgent.indexOf("compatible") > -1 &&
      userAgent.indexOf("MSIE") > -1; //判断是否IE<11浏览器
    var isEdge = userAgent.indexOf("Edge") > -1 && !isIE; //判断是否IE的Edge浏览器
    var isIE11 =
      userAgent.indexOf("Trident") > -1 &&
      userAgent.indexOf("rv:11.0") > -1;
    if (isIE || isEdge || isIE11) {
      //window.navigator.msSaveOrOpenBlob(blob, unescape(res.headers.filename.replace(/\\u/g, "%u")));
      window.navigator.msSaveOrOpenBlob(blob, unescape(name + new Date().getTime() + ".csv"));
    } else {
      var downloadElement = document.createElement("a");
      var href = window.URL.createObjectURL(blob); //创建下载的链接
      downloadElement.href = href;
      downloadElement.download = unescape(name + new Date().getTime() + ".csv"); //下载后文件名
      document.body.appendChild(downloadElement);
      downloadElement.click(); //点击下载
      document.body.removeChild(downloadElement); //下载完成移除元素
      window.URL.revokeObjectURL(href); //释放掉blob对象
    }
  });
}
